package app.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

import app.textSrc.TextSource;
import app.ui.QuestionUI;

public class QuestionUI_ControlAction extends KeyAdapter implements
		ActionListener {
	private QuestionUI mPanel;

	public QuestionUI_ControlAction(QuestionUI mPanel) {
		super();
		this.mPanel = mPanel;
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();

		switch (keyCode) {
		case KeyEvent.VK_LEFT: // Back
			doBack(null);
			break;
		case KeyEvent.VK_RIGHT: // Next
			doNext(null);
			break;
		case KeyEvent.VK_SPACE:
			break;
		default:
			break;
		}

	}

	public void actionPerformed(ActionEvent e) {
		JButton jButton = (JButton) e.getSource();

		if (jButton.getText().equals(mPanel.getBtnLables()[5])) { // next button
			doNext(jButton);
		} else if (jButton.getText().equals(mPanel.getBtnLables()[4])) { // back
																			// button
			doBack(jButton);
		} else if (jButton.getText().equals(mPanel.getBtnLables()[2])) { // answer
			doAnswer(jButton);
		} else if (jButton.getText().equals(mPanel.getBtnLables2()[1])) { // reset
			doReset(jButton);
		} else if (jButton.getText().equals(mPanel.getBtnLables()[3])) { // home
			mPanel.close(false);
		} else if (jButton == mPanel.getBtnShowTS()) {
			mPanel.showTapeScript(!mPanel.getjScrollPane().isVisible());
		}
	}

	private void doBack(JButton jButton) {
		int index = mPanel.getIndex() - 1;
		if (index >= 0) {
			mPanel.setIndex(index);
			mPanel.initializeQuestion();
			mPanel.resizeComponents();
			rememberAnswers();
		} else {
			JOptionPane.showMessageDialog(mPanel, "Begining!");
		}
	}

	private void doNext(JButton jButton) {
		int index = mPanel.getIndex() + 1;
		if (index < mPanel.getMaxIndex()) {
			mPanel.setIndex(index);
			mPanel.initializeQuestion();
			mPanel.resizeComponents();
			rememberAnswers();
		} else {
			JOptionPane.showMessageDialog(mPanel, "Ending!");
		}
	}

	private void doAnswer(JButton jButton) {
		// change the background
		mPanel.setShowAnsewr(true);
		rememberAnswers();

		// show message
		int numberOfWrongAnswer = mPanel.getTextSource().compareAnser();
		String str1 = "Correct: " + (mPanel.getTextSource().getAmtQue() - numberOfWrongAnswer);
		String str2 = "Incorrect: " + numberOfWrongAnswer;

		mPanel.getmFrame().setTitle(str1 + "    " + str2);
		JOptionPane.showMessageDialog(mPanel, str1 + "\n" + str2);

		// check the amount of custom answers
		String[] cusAns = mPanel.getTextSource().getCusAnsStr();
		int num = 0;
		for (String s : cusAns) {
			if (s != null) {
				num++;
			}
		}
		if (mPanel.getTextSource().getAmtQue() != num) {
			mPanel.setShowAnsewr(false);
		} else {
			// rename button
			jButton.setText(mPanel.getBtnLables2()[1]); // reset
		}
	}

	private void doReset(JButton jButton) {
		jButton.setText(mPanel.getBtnLables()[2]); // answer

		if (mPanel.isShowAnsewr()) {
			mPanel.setShowAnsewr(false);
		}

		int num = mPanel.getTextSource().getCusAnsStr().length;
		for (int i = 0; i < num; i++) {
			mPanel.getTextSource().getCusAnsStr()[i] = null;
		}
		mPanel.initializeQuestion();
	}

	// private void rememberAnswers() {
	// JCheckBox[][] jCheckBoxs = mPanel.getjCheckBoxes();
	// int index = mPanel.getIndex() * 3;
	// String corAns = null;
	// String cusAns = null;
	// // remember last selecting.
	// for (int i = 0; i < 3; i++) {
	// cusAns = mPanel.getTextSource().getCusAnsStr()[index + i];
	// int j = -1;
	// if (cusAns != null) {
	// j = TextSource.ansLetStr.indexOf(cusAns);
	// jCheckBoxs[i][j].setFont(mPanel.getmFrame().getAppInit().getFont3());
	// jCheckBoxs[i][j].setSelected(true);
	// jCheckBoxs[i][j].setBackground(mPanel.getmFrame().getAppInit().getCol2());
	// }
	// if (mPanel.isShowAnsewr()) {
	// corAns = mPanel.getTextSource().getCorAnsStr()[index + i];
	// int j2 = -2;
	// if (corAns != null) {
	// j2 = TextSource.ansLetStr.indexOf(corAns);
	// jCheckBoxs[i][j2].setBackground(mPanel.getmFrame().getAppInit().getCol4());
	// }
	// if (j != -1 && j != j2) {
	// jCheckBoxs[i][j].setForeground(mPanel.getmFrame().getAppInit().getCol3());
	// }
	// }
	// }
	// }

	private void rememberAnswers() {
		JCheckBox[][] jCheckBoxs = mPanel.getjCheckBoxes();
		int index = mPanel.getIndex() * 3;
		String[] corAns = mPanel.getTextSource().getCorAnsStr();
		String[] cusAns = mPanel.getTextSource().getCusAnsStr();
		// remember last selecting.
		for (int i = 0; i < 3; i++) {
			int j = -1;
			if (cusAns[index + i] != null) {
				j = TextSource.ansLetStr.indexOf(cusAns[index + i]);
				jCheckBoxs[i][j].setFont(mPanel.getmFrame().getAppInit().getFont3());
				jCheckBoxs[i][j].setSelected(true);
				jCheckBoxs[i][j].setBackground(mPanel.getmFrame().getAppInit().getCol2());

				if (mPanel.isShowAnsewr()) {
					int j2 = -2;
					if (corAns != null) {
						j2 = TextSource.ansLetStr.indexOf(corAns[index + i]);
						jCheckBoxs[i][j2].setBackground(mPanel.getmFrame().getAppInit().getCol4());
					}
					if (j != -1 && j != j2) {
						jCheckBoxs[i][j].setForeground(mPanel.getmFrame().getAppInit().getCol3());
					}
				}
			}
		}
	}
}
